package com.example.auv_660.moduleload;

import com.example.auv_660.MainActivity;
import com.example.auv_660.network.tcp.ModuleReadHandler;
import com.example.auv_660.network.tcp.ModuleWriteHandler;
import com.example.auv_660.util.Constants;
import com.example.auv_660.util.MsgType;
import com.example.auv_660.util.ThreadPool;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.Random;
import java.util.TimerTask;

public class LightLoad extends ModuleLoad {
    private ModuleWriteHandler lightWriteHandler;
    private ModuleReadHandler lightReadHandler;
    private AsynchronousSocketChannel asynchronousSocketChannel;
    private MainActivity mainActivity = MainActivity.getInstance();
    private int threadKey;

    @Override
    public void doModuleLoad(String remoteModuleIP, AsynchronousSocketChannel asynchronousSocketChannel) {
        if (remoteModuleIP.equals(Constants.LIGHT_IP))
        {
            if (this.asynchronousSocketChannel != null)
            {
                releaseModuleResource();;
            }

            this.asynchronousSocketChannel = asynchronousSocketChannel;

            if (lightWriteHandler == null)
            {
                lightWriteHandler = new ModuleWriteHandler(asynchronousSocketChannel);
            }

            if (lightReadHandler == null)
            {
                lightReadHandler = new ModuleReadHandler(asynchronousSocketChannel)
                {
                    @Override
                    public void completed(Integer result, ByteBuffer attachment)
                    {
                        if (result > 0)
                        {
                            attachment.flip();
                            byte[] bytes = new byte[attachment.remaining()];
                            attachment.get(bytes);
                            String light = null;
                            try
                            {
                                light = new String(bytes, "UTF-8");
                            }
                            catch (UnsupportedEncodingException e)
                            {
                                e.printStackTrace();
                            }

                            System.out.println(light);
                            if (mainActivity != null)
                            {
                                mainActivity.sendMsgToMainThread(MsgType.LIGHT_MSG, light.split("\\*")[0]);
                            }
                            getMessageFromModule();
                        }
                    }
                };
                lightReadHandler.getMessageFromModule();
            }

            try
            {
                if (mainActivity != null && asynchronousSocketChannel != null)
                {
                    mainActivity.sendMsgToMainThread(MsgType.LIGHT_CONN, asynchronousSocketChannel.getRemoteAddress());
                }
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }

            threadKey = ThreadPool.INSTANCE.getThreadPoolProxy().scheduledTask(new TimerTask() {
                @Override
                public void run() {
                    Random random = new Random();
                    int number = random.nextInt(3);
                    String [] content = {"R", "G", "B"};
                    System.out.println("COLOR: " + content[number]);
                    String order = Constants.AUV_LIGHT_ORDER_PREFIX + content[number] + Constants.AUV_ORDER_SUFFIX;

                    try
                    {
                        lightWriteHandler.sendCommandToModule(order);
                    }
                    catch (Exception e)
                    {

                    }
                }
            }, 3000);

            return;
        }
    }

    @Override
    public ModuleWriteHandler getWriterHandler() {
        if (lightWriteHandler == null)
        {
            if (mainActivity != null)
            {
                mainActivity.sendMsgToMainThread(MsgType.TOAST_MSG, "灯带模块未连接");
            }
        }
        return lightWriteHandler;
    }

    @Override
    public ModuleReadHandler getReadHandler() {
        if (lightReadHandler == null)
        {
            if (mainActivity != null)
            {
                mainActivity.sendMsgToMainThread(MsgType.TOAST_MSG, "灯带模块未连接");
            }
        }
        return lightReadHandler;
    }

    @Override
    public void releaseModuleResource() {
        if (this.asynchronousSocketChannel != null)
        {
            try
            {
                this.asynchronousSocketChannel.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }

        if (lightReadHandler != null)
        {
            lightReadHandler = null;
        }

        if (lightWriteHandler != null)
        {
            lightWriteHandler = null;
        }
    }
}
